package com.example.demo.controller;

import com.example.demo.entity.JobMsg;
import com.example.demo.entity.response.job.JobDetailResp;
import com.example.demo.entity.ResponseInfo;
import com.example.demo.service.JobMsgService;
import com.example.demo.utils.PageInfoNew;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronExpression;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/job")
@Api(tags = "任务管理接口")
public class JobController {

    @Resource
    JobMsgService jobMsgService;

    @PostMapping("/add")
    @ApiOperation(value = "添加任务接口")
    public ResponseInfo<String> add(@RequestBody JobMsg jobMsg) {
        if (CronExpression.isValidExpression(jobMsg.getCronExpression()) && !jobMsg.getCronExpression().isEmpty()) {
            jobMsgService.add(jobMsg);
            return ResponseInfo.successInfo("");
        } else {
            log.error(jobMsg.getCronExpression());
            return ResponseInfo.errorInfo("时间表达式有误");
        }
    }

    @GetMapping("/page")
    @ApiOperation(value = "任务分页接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度", required = true, dataType = "int"),
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "int")})
    public ResponseInfo<PageInfoNew<JobMsg>> page(@RequestParam int pageNum,
                                                  @RequestParam int pageSize,
                                                  @RequestParam int projectId) {
        return ResponseInfo.successInfo(jobMsgService.jobMsgPage(pageNum, pageSize, projectId));
    }

    @GetMapping("/detail")
    @ApiOperation(value = "任务详情接口")
    @ApiImplicitParam(name = "id", value = "detail", required = true, dataType = "int")
    public ResponseInfo<JobDetailResp> getApiDetails(@RequestParam int id) {
        return ResponseInfo.successInfo(jobMsgService.jobDetail(id));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新任务接口")
    public ResponseInfo<String> update(@RequestBody JobMsg jobMsg) {
        jobMsgService.updateJob(jobMsg);
        return ResponseInfo.successInfo("");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除任务接口")
    @ApiImplicitParam(value = "任务id", name = "id", required = true, dataType = "int")
    public ResponseInfo<String> delete(@RequestParam int id) {
        jobMsgService.deleteJob(id);
        return ResponseInfo.successInfo("");
    }

    @PostMapping("/modifyState")
    @ApiOperation(value = "修改任务状态接口")
    public ResponseInfo<String> modifyState(@RequestBody JobMsg jobMsg) {
        jobMsgService.modifyState(jobMsg);
        return ResponseInfo.successInfo("");
    }

    @PostMapping("/run")
    @ApiOperation(value = "手动执行形式任务")
    public ResponseInfo<String> run(@RequestParam int jobId) {
        jobMsgService.run(jobId);
        return ResponseInfo.successInfo("");
    }


}
